#include <iostream>
#include <vector>
#include<stack>
#include<algorithm>
using std::stack;
using std::max;
using std::string;
using std::vector;


class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        vector<int> hs(heights.size(), 0);
        stack<int> sk;
        for (int i = 0; i < heights.size(); i++) {
            hs[i] = getWidth(sk, heights, i, true);
        }
        //for (auto h: hs) std::cout << h << " ";
        std::cout << "\n";
        sk = {};
        // std::cout << "stack size: " << sk.size() << "\n";
        for (int i = heights.size()-1; i >= 0; i--) {
            auto h = getWidth(sk, heights, i, false);
            //std::cout << h << " ";
            hs[i] += h + 1;
        }
        //std::cout << "\n";
        int maxArea = 0;
        for (int i = 0; i < heights.size(); i++) {
            maxArea = max(maxArea, hs[i] * heights[i]);
        }
        return maxArea;

    }

    int getWidth(stack<int>& sk, vector<int>& heights, int idx, bool left) {
        while (!sk.empty() && (heights[sk.top()] >= heights[idx])) {
            sk.pop();
        }
        auto bound = left ? -1 : heights.size();
        if (!sk.empty()) bound = sk.top();
        sk.push(idx);
        return left ? idx - bound - 1: bound - idx - 1;

    }
};


int main() {
    vector<int> nums;
    int n;
    while (std::cin >> n) nums.push_back(n);
    for (auto num: nums) std::cout << num << " ";
    std::cout << "\n";
    
    auto res = Solution().largestRectangleArea(nums);
    std::cout << "Max Rectangle Area is " << res << "\n";
    return 0;
}